import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
import './assets/css/global.css'
// 引入Leaflet对象 挂载到Vue上，便于全局使用，也可以单独页面中单独引用
import * as L from 'leaflet'
import 'leaflet/dist/leaflet.css'
import 'leaflet.pm'
import 'leaflet.pm/dist/leaflet.pm.css'

Vue.config.productionTip = false
Vue.L = Vue.prototype.$L = L

// leaflet icon 
delete L.Icon.Default.prototype._getIconUrl
L.Icon.Default.mergeOptions({
  iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png'),
  iconUrl: require('leaflet/dist/images/marker-icon.png'),
  shadowUrl: require('leaflet/dist/images/marker-shadow.png'),
})

//路由守卫
//路由全局前置狗钩子 beforeEach
router.beforeEach(function (to, from, next) {
  let token = localStorage.getItem("hanghanghang-token");
  //如果用户访问的为login页面，直接放行。
  //如果用户访问不是login页面 ，则判断是否有token值。
  if (to.path === "/login") {
    next();
  } else {
    if (token) {
      let sideMenu = store.state.sideMenu;
      if (sideMenu && sideMenu.length === 0) {
        store.dispatch("FETCH_MENULIST").then(() => {
          next({ path: to.path });
        })
      }
      else {
        next();
      }
    } else {
      next({ path: "/login" });
    }
  }
})

Vue.use(ElementUI);
Vue.use(Antd);
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
